<?php

namespace AppBundle\Command;

use AppBundle\Task\AlertModelConfig;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class AmazingCommand extends ContainerAwareCommand
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this
            ->setName('app:task:amazing')
            ->setDescription('api notifycation.');
    }

    /**
     * {@inheritdoc}
     *
     * url params:v={消息版本}&id={消息id}&s={xrpusdt交易对}&p={价格}&lt={拉升时间}&nt={通知时间}&type={1:做多,2:做空}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $redis = $this->getContainer()->get('huobi.redis');
        while (1) {
            $content = $redis->brPop('list:kline_amazing', 10);

            $output->writeln('running...' . time());

            if (empty($content)) {
                continue;
            }

            $params = json_decode($content[1], true);
            $this->apiSync($this->syncUri(), $params, $output);
        }

    }

    private function syncUri()
    {
        $syncUri = [
            'http://api.bidao.mmdata.cn/huobi_api/trademsg/add',
            'http://deal.bjtwtx.com/api/signal',
        ];
        return $syncUri;
    }

    private function apiSync($uri, &$params = [], $output)
    {
        foreach ($uri as $api) {
            $context = stream_context_create(array(
                'http' => array(
                    'method' => 'GET',
                    'timeout' => 2 //超时时间，单位为秒
                )
            ));

            $url = $api . '?' . http_build_query($params, null, '&', PHP_QUERY_RFC3986);
            $output->writeln($url);
            $content = @file_get_contents($url, false, $context);

            $output->writeln($content);

            unset($context, $url, $content);
        }

    }
}
